package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
/**
 * 购物车
 */
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 查询购物车列表
     * @return
     *//*
    @GetMapping("/list")
    public R<List> list() {
        return R.success(new ArrayList());
    }*/

    /**
     * 查询购物车列表
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //查询当前登陆用户的购物车
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //根据创建时间正序排序
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> resultList = shoppingCartService.list(lqw);
        return R.success(resultList);
    }


    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {

        log.info("添加购物车，接收到数据 shoppingCart={}", shoppingCart);
        //1. 判断一下当前添加的菜品或者套餐在 当前登陆用的购物车中是否存在
        //1.1 根据当前登陆用户id和菜品id或者套餐id查询
        //select * from shopping_cart where user_id = 1 and dish_id = 22312/setmeal_id=12412412
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //如果前端传递过来的菜品id不为空，就根据菜品id查询
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //如果前端传递过来的套餐id不为空，就根据套餐id查询
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart shoppingCartDo = shoppingCartService.getOne(lqw);


        if (shoppingCartDo != null) {
            //2. 如果存在(当前商品在购物车中存在)？数量+1
            shoppingCartDo.setDishFlavor(shoppingCart.getDishFlavor());//覆盖口味
            shoppingCartDo.setNumber(shoppingCartDo.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartDo);
        } else {
            //3. 如果不存在，加入购物车，数量默认为1
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);

            shoppingCartDo = shoppingCart;
        }

        return R.success(shoppingCartDo);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //清空购物车
        //delete from shopping_cart where user_id = ?
        shoppingCartService.remove(lqw);

        return R.success("清空购物车成功");
    }


    /**
     * 购物车菜品或套餐数量增删
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    @Transactional
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据:{}", shoppingCart);
        //菜品id
        Long dishId = shoppingCart.getDishId();
        //套餐id
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();

        //代表数量减少的是菜品数量
        if (dishId != null) {
            //通过dishid查出购物车菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
            //查询当前用户对应的购物车
            queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            ShoppingCart cartDish = shoppingCartService.getOne(queryWrapper);
            cartDish.setNumber(cartDish.getNumber() - 1);
            Integer number = cartDish.getNumber();
            if (number > 0) {
                //对数据进行更新
                shoppingCartService.updateById(cartDish);
            } else if (number == 0) {
                //如果购物车的数量为0,那么就将菜品从购物车删除
                shoppingCartService.removeById(cartDish.getId());

            } else if (number < 0) {
                return R.error("操作异常");
            }
            return R.success(cartDish);
        }

        //代表数量减少的是套餐数量
        if (setmealId != null) {
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
            //查询当前用户对应购物车
            queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            ShoppingCart cartSetmeal = shoppingCartService.getOne(queryWrapper);
            cartSetmeal.setNumber(cartSetmeal.getNumber() - 1);
            Integer number = cartSetmeal.getNumber();
            if (number > 0) {
                //对数据进行更新
                shoppingCartService.updateById(cartSetmeal);
            } else if (number == 0) {
                //如果购物车的数量为0,那么就将taoc从购物车删除
                shoppingCartService.removeById(cartSetmeal);

            } else if (number < 0) {
                return R.error("操作异常");
            }
            return R.success(cartSetmeal);

        }
        //如果菜品和套餐都进不去
        return R.error("操作异常");
    }

}